package com.hthy.futures.ui.fragment;

import android.view.View;

import com.hthy.futures.R;
import com.hthy.futures.api.ST_TradeApi;
import com.hthy.futures.base.BaseListFragment;
import com.hthy.futures.base.CommonAdapter;
import com.hthy.futures.base.CommonViewHolder;
import com.hthy.futures.bean.MoneyOutInData;
import com.hthy.futures.utils.ErrorUtils;
import com.hthy.futures.utils.LogUtils;
import com.hthy.futures.utils.StringUtils;
import com.hthy.futures.view.EmptyLayout;
import com.vilyever.socketclient.SocketClient;

import net.hthy.bean.Trade;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import cn.bingoogolapple.refreshlayout.BGARefreshLayout;

/**
 * 出入金
 * Author:  Mr.bingley
 * Version:
 * Date:  2017/3/6
 */

public class EntryExitFragment extends BaseListFragment {

    private CommonAdapter<MoneyOutInData> mEntryExitAdapter;


    @Override
    protected void initWidget(View root) {
        super.initWidget(root);

        getEntryExitData();

        //'1', 入金 '2', 出金 '3', 红冲 '4', 蓝补 '5', 签约 '6', 解约 '7', 调账入金 '8', 调账出金 '9'  交易所手续费结转
        mEntryExitAdapter = new CommonAdapter<MoneyOutInData>(getActivity(), R.layout.adapter_entry_exit) {
            @Override
            public void convert(CommonViewHolder vh, MoneyOutInData item) {
                vh.setText(R.id.tv_trade_number, "单号：" + item.get_$8116());
                vh.setText(R.id.tv_money, "金       额：" + StringUtils.double_df.format(StringUtils.toDouble(item.getDbTransAmount())));

                // 出入金流水状态'0' //已受理'1' //支付成功'2' //系统拒绝 '3' //银行处理中
                int item_$150 = item.getUiProcessStatus();
                switch (item_$150) {
                    case '0':
                        vh.setText(R.id.tv_trade_state, "状态:" + "已受理");
                        break;
                    case '1':
                        vh.setText(R.id.tv_trade_state, "状态:" + "支付成功");
                        break;
                    case '2':
                        vh.setText(R.id.tv_trade_state, "状态:" + "系统拒绝");
                        break;
                    case '3':
                        vh.setText(R.id.tv_trade_state, "状态:" + "银行处理中");
                        break;
                }


                //'1', 入金 '2', 出金 '3', 红冲 '4', 蓝补 '5', 签约 '6', 解约 '7', 调账入金 '8', 调账出金 '9'  交易所手续费结转
                int item_$8031 = item.getUiTransType();
                switch (item_$8031) {
                    case '1':
                        vh.setText(R.id.tv_type, "类        型：" + "入金");
                        break;
                    case '2':
                        vh.setText(R.id.tv_type, "类        型：" + "出金");
                        break;
                    case '3':
                        vh.setText(R.id.tv_type, "类        型：" + "红冲");
                        break;
                    case '4':
                        vh.setText(R.id.tv_type, "类        型：" + "蓝补");
                        break;
                    case '5':
                        vh.setText(R.id.tv_type, "类        型：" + "签约");
                        break;
                    case '6':
                        vh.setText(R.id.tv_type, "类        型：" + "解约");
                        break;
                    case '7':
                        vh.setText(R.id.tv_type, "类        型：" + "调账入金");
                        break;
                    case '8':
                        vh.setText(R.id.tv_type, "类        型：" + "调账出金");
                        break;
                    case '9':
                        vh.setText(R.id.tv_type, "类        型：" + "交易所手续费结转");
                        break;
                    case 'A':
                        vh.setText(R.id.tv_type, "类        型：" + "查询最大可出金额");
                        break;
                    case 'B':
                        vh.setText(R.id.tv_type, "类        型：" + "修改签约信息");
                        break;
                }


                vh.setText(R.id.tv_request_time, "请求时间:  " + item.getSsApplyTime());
                vh.setText(R.id.tv_review_time, "审核时间:  " + item.getSsApproveTime());
            }

        };

        listView.setAdapter(mEntryExitAdapter);
    }

    @Override
    public void onBGARefreshLayoutBeginRefreshing(BGARefreshLayout bgaRefreshLayout) {
        super.onBGARefreshLayoutBeginRefreshing(bgaRefreshLayout);
        getEntryExitData();
    }

    /**
     * 出入金请求
     */
    private void getEntryExitData() {
        byte[] bytes = ST_TradeApi.searchGoods("2019");
        socketClient.sendData(bytes);
    }

    @Override
    protected void onResponse(SocketClient client, String responMes, byte[] subBytes) {
        super.onResponse(client, responMes, subBytes);
        LogUtils.e(responMes);

        try {

            Trade.QueryTransResponseMsg queryTransResponseMsg = Trade.QueryTransResponseMsg.parseFrom(subBytes);
            int iErrorCode = queryTransResponseMsg.getResponseMsg().getIErrorCode();
            if (0 == iErrorCode) {   // 0说明是正确返回的
                endResponse();
                List<Trade.UserTransDetail> userTransDetailList = queryTransResponseMsg.getUserTransDetailList();
                if (userTransDetailList != null && userTransDetailList.size() > 0) {
                    // 得进行json非空判断
                    List<MoneyOutInData> datas = new ArrayList<>();

                    for (Trade.UserTransDetail userTransDetail : userTransDetailList) {
                        MoneyOutInData moneyOutInData = new MoneyOutInData();
                        moneyOutInData.set_$8116("1111111111");    // TODO
                        moneyOutInData.setUiProcessStatus(userTransDetail.getUiProcessStatus());
                        moneyOutInData.setDbTransAmount(userTransDetail.getDbTransAmount()+"");
                        moneyOutInData.setUiTransType(userTransDetail.getUiTransType());
                        moneyOutInData.setSsApplyTime(userTransDetail.getSsApplyTime());
                        moneyOutInData.setSsApproveTime(userTransDetail.getSsApproveTime());
                        datas.add(moneyOutInData);
                    }
                    if (mEntryExitAdapter.getDatas().size() > 0) {
                        mEntryExitAdapter.clear();
                    }
                    Collections.reverse(datas);
                    mEntryExitAdapter.addItem(datas);
                } else {
                    errorLayout.setErrorType(EmptyLayout.NODATA);
                }
            } else {
                ErrorUtils.sendErrorMes(getActivity(), iErrorCode + "");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
